<?php
namespace app\zhihui\controller;

use app\common\model\ZhMemberLog as ZhMemberLogModel;
use app\common\model\MemberLoginIp as MemberLoginIpModel;
use app\common\model\ZhMemberAlllog as ZhMemberAlllogModel;
use app\home\controller\Base;
use think\Db;
/**
 * 添加用户记录控制器
 * Class Zhmonks
 * @package app\index\controller
 */
class Zhmemberlog extends Base
{
    /**
     * [add  添加]
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new ZhMemberLogModel;
        $amodel = new ZhMemberAlllogModel;
        $imodel = new MemberLoginIpModel;
        $user = $this->user;
        $map = [];
        try{
            $date = strtotime(date('Y-m-d',time()));
            $map['create_time'] = ['between',[$date,$date+24*60*60]];
            $map['mid'] = $user['id'];
            $log = $model->findOne($map);
            $ip = getIP();
            $address = getipaddress($ip);
            $address = json_decode($address,true); //数据转换

            if(!empty($user['tel'])){
                $type = getphonetype($user['tel']);
            }else{
                $type = 4;
            }
            $ipaddr = $imodel->findData($user['id'],'mid');
            if(empty($ipaddr)){
                $imodel->addData(['mid'=>$user['id'],'ip'=>$ip,'province'=>$address['content']['address_detail']['province'],'city'=>$address['content']['address_detail']['city'],'operator'=>$type]);
            }else{
                $imodel->editData(['id'=>$ipaddr['id'],'ip'=>$ip,'province'=>$address['content']['address_detail']['province'],'city'=>$address['content']['address_detail']['city'],'operator'=>$type]);
            }

            $amodel->addData(['mid'=>$user['id'],'comid'=>$param['comid'],'ip'=>$ip,'province'=>$address['content']['address_detail']['province'],'city'=>$address['content']['address_detail']['city'],'operator'=>$type,'page'=>$param['page']]);
            Db::name('company')->where('id',$param['comid'])->setInc('alllog');
            if(empty($log)){
                $model->addData(['mid'=>$user['id'],'comid'=>$param['comid'],'ip'=>$ip,'province'=>$address['content']['address_detail']['province'],'city'=>$address['content']['address_detail']['city'],'operator'=>$type]);
                Db::name('company')->where('id',$param['comid'])->setInc('log');
            }else{
                return admin_json(1, '今日已添加');
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
}